package com.practice.draw

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentManager
import android.support.v4.app.FragmentPagerAdapter

import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    private val pageModels: MutableList<Pair<Int, Int>> = mutableListOf()

    init {
        pageModels.add(Pair(R.string.title_draw_color, R.layout.layout_draw_color))
        pageModels.add(Pair(R.string.title_draw_circle, R.layout.layout_draw_circle))
        pageModels.add(Pair(R.string.title_draw_rect, R.layout.layout_draw_rect))
        pageModels.add(Pair(R.string.title_draw_point, R.layout.layout_draw_point))
        pageModels.add(Pair(R.string.title_draw_oval, R.layout.layout_draw_oval))
        pageModels.add(Pair(R.string.title_draw_line, R.layout.layout_draw_line))
        pageModels.add(Pair(R.string.title_draw_round_rect, R.layout.layout_draw_round_rect))
        pageModels.add(Pair(R.string.title_draw_arc, R.layout.layout_draw_arc))
        pageModels.add(Pair(R.string.title_draw_path, R.layout.layout_draw_path))
        pageModels.add(Pair(R.string.title_draw_histogram, R.layout.layout_draw_bar_chart))
        pageModels.add(Pair(R.string.title_draw_pie_chart, R.layout.layout_draw_pie_chart))
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        initView()
    }

    private fun initView() {
        view_pager.offscreenPageLimit = 2
        view_pager.adapter = DrawPagerAdapter(supportFragmentManager)
        toolbar_tab.setupWithViewPager(view_pager)
    }

    inner class DrawPagerAdapter(fm: FragmentManager?) : FragmentPagerAdapter(fm) {

        override fun getItem(position: Int): Fragment {
            val pageModel = pageModels[position]
            return PageFragment.newInstance(pageModel.second)
        }

        override fun getCount() = pageModels.size

        override fun getPageTitle(position: Int): CharSequence? {
            val pageModel = pageModels[position]
            return getString(pageModel.first)
        }
    }
}
